<template>
	<view>
		<u-form :model="form" id="form-container">
			<u-form-item label="员工工号" :label-width="labelWidth">
				<u-input v-model="form.job_number" placeholder="请输入员工工号" />
			</u-form-item>
			<u-form-item label="关键字查询" class="u-flex u-flex-nowrap" :label-width="labelWidthType">
				<div class="u-flex u-flex-nowrap u-row-between">
					<u-input v-model="form.keyWord" />
					<u-button type="success" size="default" @click="queryKeyWord" class="queryKeyWord">查询</u-button>
				</div>
			</u-form-item>
			<u-form-item label="事项描述" :label-width="labelWidth">
				<u-input v-model="form.performanceDesc" :placeholder="defaultReminder" type="textarea" disabled />
			</u-form-item>
			<u-form-item label="事项编号" :label-width="labelWidth">
				<u-input v-model="form.performanceCode" :placeholder="defaultReminder" disabled />
			</u-form-item>
			<u-form-item label="分数" :label-width="labelWidth">
				<u-input v-model="form.score" :placeholder="defaultReminder" disabled />
			</u-form-item>
			<u-form-item class="u-flex u-row-center">
				<u-button type="success" size="medium" @click="postForm">提交</u-button>
			</u-form-item>
		</u-form>
		<!-- <u-action-sheet :list="keyList" v-model="popShow" @click="problemNatureSelect"></u-action-sheet> -->
		<!-- safe-area-inset-bottom开启底部安全区适配 -->
		<u-select v-model="popShow" :list="keyList" @confirm="problemNatureSelect" safe-area-inset-bottom="true"></u-select>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import { getPerformanceInfo,accessPersonInfo,saveGroupLeaderEvaluate } from '../../../utils/index.js'
	export default {
		data() {
			return {
				form: {
					job_number: '',					//工段编号
					performanceDesc: '',			//事项描述
					performanceCode: '',			//事项编号
					score: '',						//分数
					keyWord: ''	
				},
				labelWidth: 140,					//表单每一项的宽度
				keyList: '',						//后台传递过来的对象经过修改键值赋值给keyList数组
				popShow: false,						//弹出层的显示控制条件
				defaultReminder: '请通过输入关键字查询',
				proList: [],						//后台传递过来的原始对象数组
				userInfo: ''
			}
		},
		methods: {
			async queryKeyWord() {
				const { data } = await getPerformanceInfo({keyWord: this.form.keyWord})
				if(data.code == 1) {
					this.proList = data.data
					this.keyList = this.changeKeyValue(data.data)
					this.popShow = true
				} else {
					console.log("查询失败")
				}
			},
			// 修改后台传递过来参数的键值，改performanceDesc为text
			changeKeyValue(data) {
				var newData = []								//经过处理返回的数据
				data.forEach((item,index) => {
					let obj = {}
					obj.label = item.performanceDesc
					obj.value = index
					newData.push(obj)
				})
				return newData
			},
			//选择相应的选项，并且填入表单
			problemNatureSelect(index) {
				let idx = index[0].value
				this.form.performanceCode = this.proList[idx].performanceCode
				this.form.score = this.proList[idx].score
				this.form.performanceDesc = this.proList[idx].performanceDesc
			},
			// 上传员工绩效奖惩表单
			async postForm() {
				if (!this.form.job_number) return this.$refs.uTips.show({
					title: '请输入员工工号',
					type: 'warning',
					duration: '2000'
				})
				if (!this.form.performanceCode) return this.$refs.uTips.show({
					title: '请选择评分事项',
					type: 'warning',
					duration: '2000'
				})
				const query = {}
				var getDate = new Date;
				let y = getDate.getFullYear()
				let m = getDate.getMonth() + 1
				let d = getDate.getDate()
				let h = getDate.getHours()
				let min = getDate.getMinutes()
				let se = getDate.getSeconds()
				query.PerformanceCode = this.form.performanceCode
				query.jobNumber = this.form.job_number
				query.landerJobNumber = this.userInfo.jobNumber
				query.createTime = y + '-' + this.dealDate(m) + '-' + this.dealDate(d) + ' ' + this.dealDate(h) + ':' + this.dealDate(min) + ':' + this.dealDate(se)
				console.log('表单提交前',query)
				const { data } = await saveGroupLeaderEvaluate(query)
				const errMsg = data.msg
				if(data.code == 1) {
					this.$refs.uTips.show({
						title: errMsg,
						type: 'success',
						duration: '2000'
					})
				} else {
					this.$refs.uTips.show({
						title: errMsg,
						type: 'warning',
						duration: '2000'
					})
				}
			},
			dealDate(data) {
				data = data < 10 ? "0" + data : data
				return data
			},
			// 获取个人信息的函数
			// 获取员工信息赋值给form
			async getUserInfo(){
				const {data} = await accessPersonInfo()
				this.userInfo = Object.assign({},data.data)
			}
		},
		onLoad(option) {
			this.getUserInfo()
		}
	}
</script>

<style lang="scss" scoped>
.content {
	padding: 24rpx;
	text-align: center;
}
.buttonGap {
	margin-top: 20rpx;
}
.main-content {
	width: 100%;
}
.queryKeyWord{
	margin-left: 10rpx;
}
#form-container ::v-deep .u-form-item {
	margin-top: 15rpx;
	padding: 10rpx 30rpx;
}
</style>
